package com.example.myapplication

import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplication.api.RetrofitFactory
import com.example.myapplication.api.WeatherApi
import com.example.myapplication.api.WeatherRepository
import com.example.myapplication.viewModel.WeatherViewModel


class MainActivity : AppCompatActivity() {

    // 获取 ViewModel 实例
    private val viewModel: WeatherViewModel by lazy {
        WeatherViewModel(WeatherRepository(RetrofitFactory.getRetrofit().create(WeatherApi::class.java)))
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewModel.extraWeather.observe(this) {
            for (forecast in it){
                if (forecast.casts.isNotEmpty()){
                    val cast = forecast.casts[1]
                    // 根据需求展示更多的天气数据
                    val textView = TextView(this@MainActivity)
                    textView.text = "${forecast.city}" +
                            "明天的天气预报：\n" +
                            "白天天气现象：${cast?.dayweather}\n" +
                            "晚上天气现象：${cast?.nightweather}\n" +
                            "白天温度：${cast?.daytemp}℃\n" +
                            "晚上温度：${cast?.nighttemp}℃\n" +
                            "白天风向：${cast?.daywind}\n" +
                            "晚上风向：${cast?.nightwind}\n" +
                            "白天风力：${cast?.daypower}\n" +
                            "晚上风力：${cast?.nightpower}"

                    findViewById<LinearLayout>(R.id.container).addView(textView)
                }
            }

        }

        viewModel.getExtraWeather()

    }
}
